<?php

namespace App\Exports;

use App\Models\User\ChengBao;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class ChengBaoExport extends DefaultValueBinder implements FromArray,WithCustomValueBinder
{
    private $conditions;

    public function __construct(array $conditions) {
        $this->conditions = $conditions;
    }

    public function array():array  {
        $data = [
            [
                'ID',
                '导入编号',
                '购买日期',
                '三级机构',
                '所属机构',
                '决策单元',
                '险种名称',
                '保单号',
                '车牌号',
                '投保人',
                '被保人',
                '投保日期',
                '保险起期',
                '保险止期',
                '核保日期',
                '三者险保额',
                '驾乘意外险保费',
                '车上人员保费',
                '车船税',
                '含税保费',
                '不含税保费',
                '车型名称',
                '车架号（VIN）',
                '发动机号',
                '车辆种类',
                '车辆初登日期',
                '使用性质',
                '座位',
                '导入账号',
                '导入时间',
            ]
        ];



        $records = (new ChengBao())->getLst($this->conditions)->with('getGuardUser:id,name')->get();
        Log::info('ex',[$records->first()]);
        //dd($records->first());
        foreach ($records as &$record) {
            $record->account = $record->getGuardUser->name;
            unset($record['updated_at']);
            unset($record->getGuardUser);
            $data[] = $record->toArray();
        }
        return $data;
    }

    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
